using System.Reflection;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using xjjxmm.infrastructure.ToolKit;
using XjjXmm.Infrastructure.ToolKit;
using XjjXmm.Infrastructure.User;
using xjjxmm.infrastructure.Webs;
using xjjxmm.infrastructure.xjjxmmLog.LogKit;

namespace xjjxmm.infrastructure.Authorizations;

public class XjjXmmAuthorizationHandler : AuthorizationHandler<XjjXmmRequirement>
{
	private readonly ILog<XjjXmmAuthorizationHandler> _log;
	private readonly UserContext _userContext;

	public XjjXmmAuthorizationHandler(ILog<XjjXmmAuthorizationHandler> log,
		IUserContextAccessor userContextAccessor)
	{
		_log = log;
		_userContext = userContextAccessor.UserContext;
		
	}

	protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
		XjjXmmRequirement requirement)
	{

		_log.Information("XjjXmmAuthorizationHandler");
		if (context.User.Identity?.IsAuthenticated != true)
		{
			return Task.CompletedTask;
		}

		if (context.Resource is HttpContext httpContext)
		{
			context.Succeed(requirement);
		}
		
		return Task.CompletedTask;
	}
	/*if (context.Resource is HttpContext httpContext)
	{
		if(!_userContext.IsAccess)
		{
			return;
		}

		var user = await  _userService.GetCurrentUser();

		if (user == null) return;

		var path = httpContext.Request.Path;
		var method = httpContext.Request.Method;

		var hasPermission = false;
		/*var apiPermission = _apiService.GetByRoles()
		var hasPermission = user.ApiPermissions.Any(t=>
			(t.Path?.Equals(path, StringComparison.OrdinalIgnoreCase) ??false) &&
			(t.HttpMethods?.Equals(method, StringComparison.OrdinalIgnoreCase)?? false)
		);#1#

		if (hasPermission)
		{
			context.Succeed(requirement);
		}
	}
	//context.Fail();
	
	return;*/
		
}